//
//  ZJMessageSignForViewController.m
//  ZJControlsClass
//
//  Created by wandoufo on 2024/12/6.
//

#import "ZJMessageSignForViewController.h"

#import "ZJMessageSignListworking.h"
#import "ZJMessageSignworking.h"
#import "ZJMessageSignListModel.h"

@interface ZJMessageSignForViewController ()  <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UILabel *countLabel;        // 数量统计标签
@property (nonatomic, strong) UILabel *numLabel;        // 数量统计标签
@property (nonatomic, strong) UIView *headerView;         // 表格头部视图
@property (nonatomic, strong) UITableView *tableView;     // 列表视图
@property (nonatomic, strong) UIButton *confirmButton;    // 确认按钮
//@property (nonatomic, assign) NSInteger pageNo;
//@property (nonatomic, assign) NSInteger pageSize;
@property (nonatomic,strong) NSMutableArray * dataSource;
@end

@implementation ZJMessageSignForViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"待签收证件明细";
//    self.view.backgroundColor = [UIColor colorWithHexString:@"#F4F4F4"];
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self setupUI];
    [self getDataSourceList];
}


- (void)getDataSourceList {
    
    
    
    WeakSelf(self)
    //    __weak typeof(self) weakSelf = self;
    [[ZJMessageSignListworking new] dispatchTaskWithParam:@{@"signBatchNo": self.model.otherData[@"signBatchNo"]} completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
        if (responseObject && [responseObject[@"code"] integerValue] == 200) {
//            if (weakSelf.pageNo == 1) {
                [weakSelf.dataSource removeAllObjects];
//            }
//            NSArray * data = responseObject[@"data"][@"list"];
//            if (data.count < 20)
                [self.tableView.mj_footer endRefreshingWithNoMoreData];
//            else [weakSelf.tableView.mj_footer endRefreshing];
            self.numLabel.text = [NSString stringWithFormat:@"%@",responseObject[@"data"][@"pendingSignCount"]];
            [weakSelf.dataSource  addObjectsFromArray:[ZJMessageSignListModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"][@"list"]]];
               
//            if (self.dataSource.count > 0) {
               
            NSInteger pendingSignCount = [responseObject[@"data"][@"pendingSignCount"] integerValue];
            self.confirmButton.hidden = !(pendingSignCount > 0 );

            
//            }
            
            [self.tableView reloadData];
        }else{
            [LUtilityTools showAlertWithTitle:@"温馨提示" message:responseObject[@"msg"] buttonAction:^{
                
            }];
        }
    }];
}


    

- (void)setupUI {
    // 创建数量统计标签
    UILabel *countLabel = [[UILabel alloc] init];
    countLabel.text = @"通行证待签收数量：";
    countLabel.font = [UIFont systemFontOfSize:12];
    countLabel.textColor = [UIColor colorWithHexString:@"666666"];
    [self.view addSubview:countLabel];
    self.countLabel = countLabel;
    
    
    UILabel *numLabel = [[UILabel alloc] init];
    numLabel.text = @"0";
    numLabel.font = [UIFont systemFontOfSize:12 weight:UIFontWeightMedium];
    numLabel.textColor = [UIColor colorWithHexString:@"181818"];
    [self.view addSubview:numLabel];
    self.numLabel = numLabel;
    
    
    // 创建表格头部
    [self setupHeaderView];
    
    // 创建列表
    UITableView *tableView = [[UITableView alloc] init];
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    tableView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:tableView];
    self.tableView = tableView;
    
    // 创建确认按钮
    UIButton *confirmButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [confirmButton setTitle:@"确认签收" forState:UIControlStateNormal];
    [confirmButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    confirmButton.backgroundColor = [UIColor colorWithHexString:@"#3271ED"];
    confirmButton.layer.cornerRadius = 8;
    confirmButton.hidden = true;
    [confirmButton addTarget:self action:@selector(confirmButtonClicked) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:confirmButton];
    self.confirmButton = confirmButton;
    
    // 设置约束
    [countLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(12 + StatusBarHeight);
        make.left.equalTo(self.view).offset(12);
        //        make.right.equalTo(self.view).offset(-12);
        //        make.height.mas_equalTo(30);
    }];
    
    // 设置约束
    [numLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        //        make.top.equalTo(self.view).offset(12 + StatusBarHeight);
        make.left.equalTo(countLabel.mas_right).offset(4);
        make.centerY.equalTo(countLabel);
        //        make.right.equalTo(self.view).offset(-12);
        //        make.height.mas_equalTo(30);
    }];
    
    
    [self.headerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(countLabel.mas_bottom).offset(12);
        make.left.equalTo(self.view).offset(12);
        make.right.equalTo(self.view).offset(-12);
        make.height.mas_equalTo(44);
    }];
    
    [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.headerView.mas_bottom);
        make.left.equalTo(self.view).offset(12);
        make.right.equalTo(self.view).offset(-12);
        make.bottom.equalTo(confirmButton.mas_top).offset(-10);
    }];
    
    [confirmButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(15);
        make.right.equalTo(self.view).offset(-15);
        make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom).offset(-10);
        make.height.mas_equalTo(44);
    }];
}

- (void)setupHeaderView {
    UIView *headerView = [[UIView alloc] init];
    headerView.backgroundColor = [UIColor colorWithHexString:@"#F0F0F0"];
    [self.view addSubview:headerView];
    self.headerView = headerView;
    
    NSArray *titles = @[@"姓名", @"通行证类型", @"通行证编号"];
    CGFloat width = UIScreen.mainScreen.bounds.size.width / 3;
    
    for (NSInteger i = 0; i < titles.count; i++) {
        UILabel *label = [[UILabel alloc] init];
        label.text = titles[i];
        label.textAlignment = NSTextAlignmentCenter;
        label.font = [UIFont systemFontOfSize:14 weight:UIFontWeightMedium];
        label.textColor = [UIColor colorWithHexString:@"#181818"];
        [headerView addSubview:label];
        
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.bottom.equalTo(headerView);
            make.left.equalTo(headerView).offset(width * i);
            make.width.mas_equalTo(width);
        }];
    }
}

#pragma mark - UITableViewDataSource & UITableViewDelegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSource.count; // 示例数据数量
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellID = @"SignForCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        [self setupCell:cell];
    }
    
    // 配置cell数据
    [self configureCell:cell atIndexPath:indexPath];
    
    
    return cell;
}

- (void)setupCell:(UITableViewCell *)cell {
    CGFloat width = UIScreen.mainScreen.bounds.size.width / 3;
    
    for (int i = 0; i < 3; i++) {
        UILabel *label = [[UILabel alloc] init];
        label.tag = 1000 + i;
        label.textAlignment = NSTextAlignmentCenter;
        label.font = [UIFont systemFontOfSize:12];
        label.textColor = [UIColor colorWithHexString:@"#141518"];
        [cell.contentView addSubview:label];
        
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.bottom.equalTo(cell.contentView);
            make.left.equalTo(cell.contentView).offset(width * i);
            make.width.mas_equalTo(width);
        }];
    }
}

- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {
    UILabel *nameLabel = [cell.contentView viewWithTag:1000];
    UILabel *typeLabel = [cell.contentView viewWithTag:1001];
    UILabel *numberLabel = [cell.contentView viewWithTag:1002];
    cell.backgroundColor = [UIColor whiteColor];
    ZJMessageSignListModel * model = self.dataSource[indexPath.row];
    nameLabel.text =model.nickname;
    typeLabel.text =  model.type;//@"人员长期通行证";
    numberLabel.text = model.idCode; //@"A099873";
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 44;
}

#pragma mark - Actions

- (void)confirmButtonClicked {
    // 处理确认签收事件
    [LUtilityTools showAlertWithTitle:@"确认签收提示" message:@"确认签收吗?" cancelTitle:@"取消" buttonAction:^{
            
        } sureTitle:@"确认" ButtonAction:^{
            WeakSelf(self)
            [[ZJMessageSignworking new] dispatchTaskWithParam:@{@"signBatchNo": self.model.otherData[@"signBatchNo"],@"type":self.model.otherData[@"type"]} completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
                if (responseObject && [responseObject[@"code"] integerValue] == 200) {
                
                    
                    weakSelf.confirmButton.hidden = true;
                }else{
                    [LUtilityTools showAlertWithTitle:@"温馨提示" message:responseObject[@"msg"] buttonAction:^{
                        
                    }];
                }
            }];
        }];

   
}
-(NSMutableArray *)dataSource{
    if (!_dataSource) {
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}

@end
